package com.idearworld.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.idearworld.dao.UserInfoMapper;
import com.idearworld.model.UserInfo;
import com.idearworld.model.UserInfoExample;
import com.idearworld.service.UserInfoService;

@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService{

	private UserInfoMapper userInfoMapper;
	
	
	@Override
	public int registerUser(UserInfo userInfo) {
		if(userInfo == null){
			return -1;
		}
		if(this.registerCheckUserEmail(userInfo.getUserAccount())){
			return -2;
		}
		if(this.registerCheckUserName(userInfo.getUserName())){
			return -3;
		}
		return this.userInfoMapper.insert(userInfo);
	}

	@Override
	public UserInfo getUser(String userName, String password) {
		UserInfoExample example =new UserInfoExample();
		example.or().andUserNameEqualTo(userName).andPasswordEqualTo(password);
		List<UserInfo> userInfoList=this.userInfoMapper.selectByExample(example);
		if(userInfoList==null && userInfoList.size()==0){
			example =new UserInfoExample();
			example.or().andUserAccountEqualTo(userName).andPasswordEqualTo(password);
			userInfoList=this.userInfoMapper.selectByExample(example);
		}
		if(userInfoList!=null && userInfoList.size()==1){
			return userInfoList.get(0);
		}
		return null;
	}

	public UserInfoMapper getUserInfoMapper() {
		return userInfoMapper;
	}

	 @Autowired
	public void setUserInfoMapper(UserInfoMapper userInfoMapper) {
		this.userInfoMapper = userInfoMapper;
	}

	@Override
	public boolean registerCheckUserName(String userName) {
		UserInfoExample example=new UserInfoExample();
		example.or().andUserNameEqualTo(userName);
	    List<UserInfo> res=userInfoMapper.selectByExample(example);
	    if(res !=null && res.size()>0){
	    	return true;
	    }
		return false;
	}

	@Override
	public boolean registerCheckUserEmail(String email) {
		UserInfoExample example=new UserInfoExample();
		example.or().andUserAccountEqualTo(email);
	    List<UserInfo> res=userInfoMapper.selectByExample(example);
	    if(res !=null && res.size()>0){
	    	return true;
	    }
		return false;
	}

	
	
	
}
